using FluentNHibernate.Mapping;
using Sample.Domain;

namespace Sample.Persistence.Mappings
{
    public sealed class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Id(p => p.Email).GeneratedBy.Assigned();
            Map(p => p.FirstName).Not.Nullable().WithLengthOf(50);
            Map(p => p.LastName).Not.Nullable().WithLengthOf(50);
            Map(p => p.Active).Not.Nullable();

            Component(p => p.Location,
                      l =>
                          {
                              l.Map(p => p.Description).WithLengthOf(50).Not.Nullable();
                              l.Map(p => p.Latitude);
                              l.Map(p => p.Longitude);
                          });

            HasMany(x => x.ReviewedIn)
                .KeyColumnNames.Add("RevieweeId")
                .Inverse()
                .FetchType.Join();

            HasMany(x => x.PeerReviewerFor)
                .KeyColumnNames.Add("ReviewerId")
                .AsBag()
                .Inverse(); // Have to save PeerReview to persist to database...
        }
    }
}